package com.android.mymedicine.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.mymedicine.R;
import com.android.mymedicine.application.UserApplication;
import com.android.mymedicine.application.impl.UserApplicationImpl;
import com.android.mymedicine.domain.user.User;
import com.android.mymedicine.infrastructure.SQLiteManager;
import com.android.mymedicine.util.ResponseData;

public class MainActivity extends Activity{
	
	private UserApplication userApplication;
	private User user;
	
	private EditText editUserName;
	private EditText editPassword;
	private Button btEnter;
	
	private ResponseData<User> rd;
	
	public MainActivity() {
		SQLiteManager.init(this);
		userApplication = new UserApplicationImpl();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		editUserName = (EditText) findViewById(R.id.editUserName);
		editPassword = (EditText) findViewById(R.id.editPassword);
		btEnter = (Button) findViewById(R.id.btEnter);
		setContentView(R.layout.loginview);
	}
	
	public void login(){
		user = new User();
		user.setUserName(editUserName.getText().toString());
		user.setPassword(editPassword.getText().toString());
		
		rd = userApplication.doLogin(user);
		if(rd.wasSuccessful()){
			Toast.makeText(this, rd.getMessage(), Toast.LENGTH_SHORT);
			startActivity(new Intent(this, DashboardActivity.class));
		}else{
			Toast.makeText(this, rd.getMessage(), Toast.LENGTH_SHORT);
		}
	}
	
	public void configureButtonEnter(){
		btEnter.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				createUser();
			}
		});
	}
	
	public void createUser(){
			
		user = new User();
		user.setUserName("Felipe");
		user.setPassword("123");
		
		rd = userApplication.create(user);
		
		if(rd.wasSuccessful()){
			Toast.makeText(this, rd.getMessage() ,Toast.LENGTH_SHORT).show();
		}else{
			Toast.makeText(this, rd.getMessage(), Toast.LENGTH_SHORT).show();
		}
	}
}
